<?php

namespace app\admin\controller\subject;

use app\common\controller\Backend;

class Order extends Backend
{
    // 定义模型的名字
    protected $model = null;

    // 开启联表查询
    protected $relationSearch = true;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Subject.Order');
    }

    // 查看
    public function index()
    {
        if ($this->request->isAjax())
        {
            // php解构语法
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $total = $this->model
                    ->with(['subject', 'business'])
                    ->where($where)
                    ->order($sort, $order)
                    ->count();

            $list = $this->model
                    ->with(['subject', 'business'])
                    ->where($where)
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    
    // 删除
    public function del($ids = null)
    {
        $row = $this->model->select($ids);

        if (!$row)
        {
            $this->error(__('课程订单不存在'));
        }

        $result = $this->model->destroy($ids);

        if ($result === false)
        {
            $this->error($this->model->getError());
            exit;
        }

        $this->success();
    }
    
    // 回收站列表
    public function recyclebin()
    {
        if ($this->request->isAjax())
        {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $total = $this->model
                ->onlyTrashed()
                ->with(['subject', 'business'])
                ->where($where)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->onlyTrashed()
                ->with(['subject', 'business'])
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
    }

    // 还原
    public function restore($ids = null)
    {
        $row = $this->model->onlyTrashed()->select($ids);

        if (!$row)
        {
            $this->error(__('课程订单不存在'));
        }

        $result = $this->model->onlyTrashed()->where('id', 'IN', $ids)->update(['deletetime' => null]);

        if ($result === false)
        {
            $this->error($this->model->getError());
            exit;
        }

        $this->success();
    }

    // 销毁
    public function destroy($ids = null)
    {
        $row = $this->model->onlyTrashed()->select($ids);

        if (!$row)
        {
            $this->error(__('课程订单不存在'));
        }

        $result = $this->model->destroy($ids, true);

        if ($result === false)
        {
            $this->error($this->model->getError());
            exit;
        }

        $this->success();
    }
}